package com.tansuo365.test1.controller.othercontrol;

import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.tansuo365.test1.bean.otherbean.WuliaoTree;
import com.tansuo365.test1.service.otherservice.DWuliaoService;
import com.tansuo365.test1.service.otherservice.WuliaoTreeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/admin/wuliaoTree")
public class WuliaoTreeController {

    @Autowired
    private WuliaoTreeService wuliaoTreeService;
    @Autowired
    private DWuliaoService dWuliaoService;

    @PostMapping("/loadWuliaoTree")
    public String loadWuliaoTree(){
        return getAllByParentId(-1).toString();
    }

    /**
     * 新模式: 获取导入后的物料表的大类, group by 同时distinct 选回 大类 集合
     * 根据大类集合,创建多个物料tree的一级分类,同时点击物料tree选择时,按物料tree名称->展示不同大类list
     *
     * @param wuliaoTree
     * @return
     */
    @RequestMapping("/save")
    public Map<String,Object> save(WuliaoTree wuliaoTree){
        Map<String, Object> resultMap = new HashMap<>();
        if(wuliaoTree.getP_id()==-1){  //一级菜单
            wuliaoTree.setIcon("icon-folderOpen");
            wuliaoTree.setState(1);
        }else { //二级菜单
            wuliaoTree.setIcon("icon-folder");
            wuliaoTree.setState(2);
        }

        wuliaoTreeService.insertSelective(wuliaoTree);
//
        resultMap.put("success", true);
        return resultMap;
    }

    @RequestMapping("/delete")
    public Map<String,Object> delete(int id){
        Map<String, Object> resultMap = new HashMap<>();
        WuliaoTree wuliaoTree=wuliaoTreeService.selectByPrimaryKey(id); //获取要删除的数据
        //查看该项目是否是一级,一级下面是否有二级
        List<WuliaoTree> secondWuliaoTreeList = wuliaoTreeService.findByParentId(wuliaoTree.getId());
        int secondSize = secondWuliaoTreeList.size();
        System.out.println("secondSize:"+secondSize);
        if(secondSize>0){ // 如果有对应二级项目,则同时一并删除
            Integer[] ids = new Integer[secondSize];
            for(int i=0; i<secondSize;i++){
                Integer everyOId = secondWuliaoTreeList.get(i).getId();
                ids[i]=everyOId;
            }
            wuliaoTreeService.deleteBatchByPKArr(ids); //批量删除二级项目分类
            dWuliaoService.deleteBatchByOidArr(ids);//批量删除一级下面的所有详细列表数据
            wuliaoTreeService.deleteByPrimaryKey(id);//删除一级项目分类
        }else{
            //假设其size不大于0,则只删除一级菜单和其对应项目列表
            wuliaoTreeService.deleteByPrimaryKey(id); // 删除
            dWuliaoService.deleteByOid(id);
        }
        resultMap.put("success", true);
        return resultMap;
    }

    /**
     * 根据父节点递归获取所有商品类别信息
     * 渲染树形第一步
     * @param parentId
     * @return
     */
    public JsonArray getAllByParentId(Integer parentId){
        JsonArray jsonArray=this.getByParentId(parentId);
        for(int i=0;i<jsonArray.size();i++){
            JsonObject jsonObject=(JsonObject) jsonArray.get(i);
            //如果是二级菜单,则不再继续添加子项
            if("second".equals(jsonObject.get("state").getAsString())){
                continue;
                //如果是一级菜单,则查找一级下面是否有二级菜单
            }else if("first".equals(jsonObject.get("state").getAsString())){
                System.out.println("建立了children子集,数据ID:"+jsonObject.get("id"));
                jsonObject.add("children", getAllByParentId(jsonObject.get("id").getAsInt()));
            }else{

            }
        }
        return jsonArray;
    }

    /**
     * 根据父节点查询子节点
     * @param p_id
     * @return
     */
    private JsonArray getByParentId(Integer p_id){
        JsonArray jsonArray=new JsonArray();
        //获取了第一组 [一级] 树形菜单
        List<WuliaoTree> wuliaoTreeList=wuliaoTreeService.findByParentId(p_id);
        for(WuliaoTree wuliaoTree:wuliaoTreeList){
            //回传的jsonArray的item
            JsonObject jsonObject=new JsonObject();
            jsonObject.addProperty("id", wuliaoTree.getId()); // 节点id
            jsonObject.addProperty("text", wuliaoTree.getWuliaofenlei()); // 节点名称
            if(wuliaoTree.getState()==1){
                jsonObject.addProperty("state", "first"); // 根节点 一级
            }else if(wuliaoTree.getState()==2){
                jsonObject.addProperty("state", "second"); // 叶子节点 二级
            }else{

            }
            jsonObject.addProperty("iconCls", wuliaoTree.getIcon());
            JsonObject attributeObject=new JsonObject(); // 扩展属性
            attributeObject.addProperty("state",wuliaoTree.getState()); // 节点状态 [一级/二级]
            jsonObject.add("attributes", attributeObject);
            jsonArray.add(jsonObject);
        }
        return jsonArray;
    }
}
